﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing.Imaging;
using System.Windows;
using iTeachSoft.CoreObjects;
using iTeachSoft.Code.Utility;

namespace iTeachSoft.Presentation.Code.Managers
{
    public class FileManager
    {
        private static string mTempProjectPath = "TempProjectPath";
        public static string RelativeImagesPath { get; private set; }
        public static string SlidePreviewImagePath { get; private set; }

        public static void ClearResources()
        {
            try
            {
                Directory.Delete(mTempProjectPath, true);
            }
            catch (Exception ex)
            {
                AlertUtility.ShowError(ex.Message);
            }
        }

        static FileManager()
        {
            mTempProjectPath = Path.Combine(System.Windows.Forms.Application.StartupPath, mTempProjectPath);
            RelativeImagesPath = "Images";
            SlidePreviewImagePath = "SlidePreview";
        }

        public static string GetSlidePreviewPath(Slide slide)
        {
            string imageName = slide.PreviewFileName;
            string imagePath = Path.Combine(mTempProjectPath, SlidePreviewImagePath);

            if (!Directory.Exists(imagePath))
                Directory.CreateDirectory(imagePath);

            return Path.Combine(imagePath, slide.PreviewFileName);
        }

        public static void SaveSlideImage(Slide slide, System.Drawing.Image slideImage)
        {
            string imageName = Guid.NewGuid().ToString()+".png";
            string imagePath = Path.Combine(mTempProjectPath, RelativeImagesPath);
            
            if (!Directory.Exists(imagePath))
                Directory.CreateDirectory(imagePath);

            int width = slide.Size.Width;
            int height = slide.Size.Height;

            System.Drawing.Image newImage = slideImage.GetThumbnailImage(width, height, null, IntPtr.Zero);

            imagePath = Path.Combine(imagePath, imageName);
            try
            {
                slideImage.Save(imagePath, ImageFormat.Png);
                newImage.Save(GetSlidePreviewPath(slide), ImageFormat.Png);
            }
            catch (Exception ex) { }

            //slide.BackgroundImage = slideImage;
            slide.BackgroundPath = imagePath;
        }
    }
}
